package com.example.androidexample.qr;

import android.content.Context;
import android.os.Build;
import android.os.VibrationEffect;
import android.os.Vibrator;

/**
 * 震动马达
 */
public class VibratingMotorController {

    private Context context;
    private final Vibrator vibrator;
    private final long VIBRATE_DURATION = 200;

    public VibratingMotorController(Context context) {
        this.context = context;
        vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
    }

    public void shotOne() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            vibrator.vibrate(VibrationEffect.createOneShot(VIBRATE_DURATION, VibrationEffect.DEFAULT_AMPLITUDE));
        } else {
            vibrator.vibrate(VIBRATE_DURATION);
        }
    }

    public void cancelShot() {
        vibrator.cancel();
    }
}
